function pMake(x, y, z) {
	particle = new THREE.Particle(material);
	position = particle.position;
	position.x = x;
	position.y = y;
	position.z = z;
	return position;
}

function pStr(position) {
	return position.x + "," + position.y + "," + position.z;
}
/*
function onDocumentMouseMove(event) {
	mouseX = event.clientX - windowHalfX;
	mouseY = event.clientY - windowHalfY;
	spot = spot + .04;
	zSpot -= .1;
}

function onDocumentTouchStart(event) {
	if(event.touches.length > 1) {
		event.preventDefault();
		mouseX = event.touches[0].pageX - windowHalfX;
		mouseY = event.touches[0].pageY - windowHalfY;
	}
}

function onDocumentTouchMove(event) {
	if(event.touches.length == 1) {
		event.preventDefault();
		mouseX = event.touches[0].pageX - windowHalfX;
		mouseY = event.touches[0].pageY - windowHalfY;
	}
}
*/
function animate() {
	requestAnimationFrame(animate);
	render();
}

function init() {

	var container, separation = 100, amountX = 50, amountY = 50, particles, particle;

	container = document.createElement('div');
	document.body.appendChild(container);

	camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 10000);
	camera.position.z = 100;

	scene = new THREE.Scene();

	scene.add(camera);

	renderer = new THREE.WebGLRenderer({
		// antialias : true,
		// clearColor : 0x000000,
		// clearAlpha : 1
	});
	//renderer = new THREE.CanvasRenderer();
	renderer.setSize(window.innerWidth, window.innerHeight);
	container.appendChild(renderer.domElement);

	// particles

	var geometry = new THREE.Geometry();

	for(var i = 0; i < 10; i++) {
		particle = new THREE.Particle(material);
		particle.position.x = Math.random() * 2 - 1;
		particle.position.y = Math.random() * 2 - 1;
		particle.position.z = Math.random() * 2 - 1;
		particle.position.normalize();
		particle.position.multiplyScalar(Math.random() * 10 + 45);
		particle.scale.x = particle.scale.y = 1;
		scene.add(particle);
		geometry.vertices.push(particle.position);
	}

	var line = new THREE.Line(geometry, new THREE.LineBasicMaterial({
		color : 0xffffff,
		opacity : 0.5
	}));
	scene.add(line);
	var geo1 = new THREE.Geometry();

	//draw the z axis
	geo1.vertices.push(pMake(0, 0, -100));
	geo1.vertices.push(pMake(0, 0, 100));
	geo1.vertices.push(pMake(0, 0, 0));

	//x-axis
	geo1.vertices.push(pMake(-100, 0, 0));
	geo1.vertices.push(pMake(100, 0, 0));
	geo1.vertices.push(pMake(0, 0, 0));

	//y-axis
	geo1.vertices.push(pMake(0, -100, 0));
	geo1.vertices.push(pMake(0, 100, 0));
	particle = new THREE.Particle(material);
	particle.position.x = 0;
	particle.position.y = 0;
	particle.position.z = 0;
	geo1.vertices.push(particle.position);
	var line1 = new THREE.Line(geo1, new THREE.LineBasicMaterial({
		color : 0xffffff,
		opacity : 0.5
	}));
	scene.add(line1);

	addText("-x", -100, 0, 0);
	addText("+x", 100, 0, 0);
	addText("-y", 0, -100, 0);
	addText("+y", 0, 100, 0);
	addText("-z", 0, 0, -100);
	addText("+z", 0, 0, 100);
	$(container).bind('mousewheel', function(event, delta) {
		camDistance += delta;
		if(camDistance < 0)
			camDistance = 0;
		return false;
	});
	$(document).mousedown(function(e) {
		startDragHandler();
		$(document).mousemove(dragHandler);
	});
	$(document).mouseup(function(e) {
		stopDragHandler();
		$(document).unbind('mousemove', dragHandler);
	});
}

function render() {
	positionCamera();
	camera.lookAt(scene.position);
	renderer.render(scene, camera);
}

function addText(label, x, y, z) {
	var text3d = new THREE.TextGeometry(label, {
		size : 8,
		height : 2,
		curveSegments : 20,
		font : "helvetiker"
	});
	text3d.computeBoundingBox();
	var textMaterial = new THREE.MeshBasicMaterial({
		color : 0xffffff,
		overdraw : true
	});
	text = new THREE.Mesh(text3d, textMaterial);
	text.doubleSided = false;
	text.position.x = x;
	text.position.y = y;
	text.position.z = z;
	parent = new THREE.Object3D();
	parent.add(text);
	scene.add(parent);
}